package cn.hebutrun.oulabot.controller;

import cn.hebutrun.oulabot.entity.BaseEntity;
import cn.hebutrun.oulabot.entity.Todo;
import cn.hebutrun.oulabot.service.ITodoService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 待办表 前端控制器
 * </p>
 *
 * @author zhaolong
 * @since 2022-09-29
 */
@Api(tags = "待办接口")
@RestController
@RequestMapping("/todo")
@RequiredArgsConstructor
public class TodoController {

  private final ITodoService iTodoService;

  @GetMapping("/list")
  @ApiOperation("获取待办列表")
  public List<Todo> list(@RequestParam String userId) {
    return iTodoService.list(
        Wrappers.lambdaQuery(Todo.class)
            .eq(Todo::getUserId,userId)
            .orderByDesc(BaseEntity::getUpdateTime)
    );
  }

  @ApiOperation("新增待办")
  @PostMapping
  public boolean post(@RequestParam String userId, @RequestParam String content) {
    return iTodoService.save(new Todo().setUserId(userId).setContent(content));
  }

  @ApiOperation("删除待办")
  @DeleteMapping
  public boolean delete(@RequestParam String id) {
    return iTodoService.removeById(id);
  }

}
